using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Media;

namespace ibba.src.sl.recursos.Validations
{
    public class ValidationManager : FrameworkElement
    {
        public class ManagerList
        {
            public FrameworkElement Element { get; set; }
            public ValidationBase Validator { get; set; }
        }

        List<ManagerList> items = new List<ManagerList>();

        public ValidationManager()
        {
            //defaults
            Indicator = new DefaultIndicator();
            this.InvalidBorder = new SolidColorBrush(Colors.Red);
            this.InvalidBorderThickness = new Thickness(1);
            //this.InvalidBackground = new SolidColorBrush(Colors.Red); ;
        }

        public void Register(FrameworkElement elementToValidate, ValidationBase v)
        {
            items.Add(new ManagerList() { Element = elementToValidate, Validator = v });
        }

        public void IsValid(FrameworkElement pElement, bool pIsValid)
        {

            ManagerList item = items.SingleOrDefault(a => a.Element.Equals(pElement));

            if (item != null)
                ((System.Windows.Controls.Control)pElement).BorderBrush = item.Validator.GetBorderColor(pIsValid);

            // ValidatorBase x = errors.SingleOrDefault(a => a.ElementToValidate.Name.Equals(item.Validator.ElementToValidate.Name));

            // if (x!= null)
            if (errors.Contains(item.Validator))
                errors.Remove(item.Validator);

            if (!pIsValid)
                errors.Add(item.Validator);

        }

        public List<ValidationBase> errors = new List<ValidationBase>();
        /// <summary>
        /// Validate all validators within the group
        /// </summary>
        /// <returns></returns>
        public List<ValidationBase> ValidateAll()
        {

            foreach (var item in items)
            {
                if (!item.Validator.Validate(true))
                    if (!errors.Contains(item.Validator))
                        errors.Add(item.Validator);
                    else
                        if (errors.Contains(item.Validator))
                            errors.Remove(item.Validator);

            }

            return errors;
        }

        public static string GetErrorString(IEnumerable<string> list, string delimiter)
        {
            string s = "";

            foreach (var item in list)
                s += item + delimiter;


            return s.TrimEnd(delimiter.ToCharArray());
        }

        public static string GetErrorString(IEnumerable<ValidationBase> list, string delimiter)
        {
            return GetErrorString(list.Select(n => n.ErrorMessage).ToList(), delimiter);
        }

        public IIndicator Indicator { get; set; }

        public Brush InvalidBackground { get; set; }

        public Brush InvalidBorder { get; set; }

        public Thickness InvalidBorderThickness { get; set; }

    }

}
